package com.lunchspothack;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class LunchspotDbHelper {
    private static final String TAG = LunchspotDbHelper.class.getSimpleName();

    private static final String DB_NAME = "lunchspot";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME = "Lunchspot";
    private static final String COLUMN_NAME = "NAME";
    private static final String COLUMN_LATITUDE = "LATITUDE";
    private static final String COLUMN_LONGITUDE = "LONGITUDE";
    
    private final DbHelper dbHelper;
    private Context context;
    private int limitNum = 20;
    
    public LunchspotDbHelper(Context context) {
    	this.context = context;
    	this.dbHelper = new DbHelper(context);
    	
    }
    
    public void insertOrIgnore(Lunchspot lunchspot) {
        ContentValues values = getContentValues(lunchspot);
        SQLiteDatabase db = this.dbHelper.getWritableDatabase();
        try {
//            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.ECLAIR_MR1) {
                // works on Android 2.1
                db.insertOrThrow(DB_NAME, null, values);
//            } else {
//                db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
//            }

        } finally {
            db.close();
        }
    }
    
    public Cursor getLunchspots() {
        SQLiteDatabase db = this.dbHelper.getReadableDatabase();
        // Return cursor where columns are
        return db.query(TABLE_NAME, null, null, null, null, null, "Order by " + COLUMN_NAME + " Limit " + limitNum);
    }
    
    public ContentValues getContentValues(Lunchspot lunchspot) {
        ContentValues values = new ContentValues();
       
        values.put(COLUMN_NAME, lunchspot.getName());
        values.put(COLUMN_LATITUDE, lunchspot.getLatitude());
        values.put(COLUMN_LONGITUDE, lunchspot.getLongitude());

        return values;
    }


    
    public SQLiteDatabase getReadableDatabase() {
        return dbHelper.getReadableDatabase();
    }

    public SQLiteDatabase getWritableDatabase() {
        return dbHelper.getWritableDatabase();
    }

    public void close() {
        this.dbHelper.close();
    }
    
    
    // DbHelper implementations
    public class DbHelper extends SQLiteOpenHelper {
        public DbHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {
            final String sql = String.format(
            	"create table %s (ID integer primary key AUTOINCREMENT, name VARCHAR, latitude VARCHAR, longitude VARCHAR)",
            	TABLE_NAME);
            db.execSQL(sql);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table " + TABLE_NAME);
            this.onCreate(db);
        }
    }

    
}
